//
//  MYPickerView.m
//  PickerView
//
//  Created by cuiwenjuan on 15-1-6.
//  Copyright (c) 2015年 anytum. All rights reserved.
//

#import "MYPickerView.h"

@implementation MYPickerView

+(instancetype)sharePickerView
{
    static MYPickerView * pickerV;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        pickerV = [[MYPickerView alloc]init];
    });
    
    return pickerV;
    
}

-(id)init
{
    if(self = [super init])
    {
        
    }
    return self;
}

-(void)createPickerView
{
    [self initPickerView];
    
    NSLog(@"%f %f %f %f ",self.frame.size.width,self.frame.size.height,self.frame.origin.x,self.frame.origin.y);
    
    picker = [[UIPickerView alloc]init];
    
    picker.frame = self.frame;
    
    picker.backgroundColor = [UIColor grayColor];
    
    picker.layer.cornerRadius = 10.0;
    
    picker.delegate = self;
    picker.dataSource = self;
    
    [self addSubview:picker];
    
}


//初始化数据
-(void)initPickerView
{
    NSBundle * bundle = [NSBundle mainBundle];
    
    NSString * plistPath = [bundle pathForResource:@"city.plist" ofType:nil];
    
    dic =[[NSArray alloc]initWithContentsOfFile:plistPath];
    
    cityArray = [[dic objectAtIndex:0] objectForKey:@"cities"];
    
    provinceArray = [[NSMutableArray alloc]init];
    
    [dic enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [provinceArray addObject:[obj objectForKey:@"state"]];
    }];
    
    province = [provinceArray objectAtIndex:0];
    
    city = [cityArray objectAtIndex:0];
    
//    NSLog(@"ooodic = %@",dic);
    
}


#pragma mark - pickerView

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}


//设置每列的宽度
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if(component == 0)
    {
        return picker.frame.size.width/3;
    }
    else
    {
        return picker.frame.size.width*2/3;
    }
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
    {
        return [provinceArray count];
    }else
    {
        return [cityArray count];
    }
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //   return [NSString stringWithFormat:@"%d,%d",(int)row,(int)component];
    
//    NSLog(@"%d  %d",(int)[provinceArray count],(int)[cityArray count]);
    
    if(component == 0)
    {
        return [provinceArray objectAtIndex:row];
    }
    else
    {
        return [cityArray objectAtIndex:row];
    }
}

//选中后的列
-(void)pickerView: (UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSLog(@"%d and %d",(int)row,(int)component);
    
    
    if(component == 0)
    {
        cityArray = [[dic objectAtIndex:row] objectForKey:@"cities"];
        
        //更新第二轮的数据
        [picker reloadComponent:1];
        //        [picker reloadComponent:2];
        
        //设置第二列自动从0 开始
        [picker selectRow:0 inComponent:1 animated:YES];
        
        province = [provinceArray objectAtIndex:row];
        
        city = [cityArray objectAtIndex:0];
    }
    else
    {
        city = [cityArray objectAtIndex:row];
    }
}



#pragma mark - 对外接口

-(NSString *)getProvince
{
    return  province;
}

-(NSString *)getCity
{
    return city;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
